<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\mill\model;

use think\Model;
use app\admin\model\Config as ConfigModel;
use app\mill\model\Give as GiveModel;
use app\mill\model\Archives as ArchivesModel;
use think\Db;

/**
 * 前端接口模型
 */
class Db2 extends Model
{

    //是否开启同步
    public static $is_open = false;
    //同步更新订单信息
    public static function orderEdit($id = '',$data = [],$id_add = false){

        if(empty(self::$is_open))return '';

        //获取客户ID
        $client = Db::name('mill_client')->where(['id'=>$data['client_id']])->find();
        $client_db2 = Db::connect('db2')->name('mill_client')->where(['nickname'=>$client['nickname'],'mobile'=>$client['mobile'],'company'=>$client['company']])->find();
        if(empty($client_db2)){
            $client_add = $client;
            unset($client_add['id']);
            $client_id = Db::connect('db2')->name('mill_client')->insertGetId($client_add);
            $client_db2 = Db::connect('db2')->name('mill_client')->where(['id'=>$client_id])->find();
        }
        $data['client_id'] = $client_db2['id'];
        $data['nickname'] = $client_db2['nickname'];
        $data['mobile'] = $client_db2['mobile'];
        $data['is_lock'] = 0;

        if($id_add){
            $db2_order_id = Db::connect('db2')->name('mill_order')->insertGetId($data);
            Db::connect('db2')->name('mill_order')->where(['id'=>$db2_order_id])->update(['order_pid'=>$db2_order_id]);
            Db::name('mill_order')->where(['id'=>$id])->update(['db2_order_id'=>$db2_order_id]);
        }
        else{
            $db2_order_id = Db::name('mill_order')->where(['id'=>$id])->value('db2_order_id');
            $order_pid = Db::connect('db2')->name('mill_order')->where(['id'=>$db2_order_id])->value('order_pid');
            Db::connect('db2')->name('mill_order')->where(['order_pid'=>$order_pid])->update($data);
        }

    }

    // 同步更新面积表信息
    public static function archivesEdit($id = '',$is_pid = '',$data = [],$archives_lists = [],$__encry__ = ''){

        if(empty(self::$is_open))return '';

        //初始化
        GiveModel::init();

        $order_id_db = $id;
        //获取客户ID
        $client = Db::name('mill_client')->where(['id'=>$data['client_id']])->find();
        $client_db2 = Db::connect('db2')->name('mill_client')->where(['nickname'=>$client['nickname'],'mobile'=>$client['mobile'],'company'=>$client['company']])->find();
        if(empty($client_db2)){
            $client_add = $client;
            unset($client_add['id']);
            $client_id = Db::connect('db2')->name('mill_client')->insertGetId($client_add);
            $client_db2 = Db::connect('db2')->name('mill_client')->where(['id'=>$client_id])->find();
        }
        $data['client_id'] = $client_db2['id'];
        $data['nickname'] = $client_db2['nickname'];
        $data['mobile'] = $client_db2['mobile'];

        //跳过审核步骤
        $data['status'] = 1;
        $data['archives_status'] = 2;
        $data['is_lock'] = 0;

        unset($data['id']);

        if($is_pid){

            $order = Db::name('mill_order')->where(['id'=>$data['order_pid']])->find();
            $data['order_pid'] = Db::connect('db2')->name('mill_order')->where(['id'=>$order['db2_order_id']])->value('order_pid');

            $re = Db::connect('db2')->name('mill_order')->insertGetId($data);
            if(!empty($re)){
                Db::name('mill_order')->where(['id'=>$id])->update(['db2_order_id'=>$re]);
                $id = $re;
            }
        }
        else {
            unset($data['order_pid']);
            $order = Db::name('mill_order')->where(['id'=>$id])->find();
            $id = $order['db2_order_id'];
            $re = Db::connect('db2')->name('mill_order')->where(['id'=>$id])->update($data);
        }
        // 更新数据
        if ($re) {

            $order = Db::connect('db2')->name('mill_order')->where(['id'=>$id])->find();
            //更新面积表
            if(!empty($archives_lists)){

                //获取所有面积表序列
                $sort_list = Db::connect('db2')->name('mill_order_archives')->where(['order_id'=>$id])->order('sort asc')->column('sort');
                if(!empty($sort_list))$sort_list = array_flip($sort_list);

                foreach ($archives_lists as $av){
                    //过滤未被删减的面积表序列
                    if(!empty($sort_list))unset($sort_list[$av['sort']]);

                    $av['order_id'] = $order['id'];
                    $av['order_pid'] = $order['order_pid'];

                    $archives_where = [
                        'order_id'  =>  $id,
                        'sort'  =>  $av['sort'],
                    ];
                    $archives = Db::connect('db2')->name('mill_order_archives')->where($archives_where)->find();
                    //录入
                    if(empty($archives)) Db::connect('db2')->name('mill_order_archives')->insertGetId($av);
                    //更新
                    else Db::connect('db2')->name('mill_order_archives')->where(['id'=>$archives['id']])->update($av);
                }
                //清除删减的面积表序列
                if(!empty($sort_list)){
                    $sort_list = array_keys($sort_list);
                    $sort_ids = Db::connect('db2')->name('mill_order_archives')->where([['order_id','=',$id],['sort','in',implode(',',$sort_list)]])->order('sort asc')->column('id');
                    if(!empty($sort_ids)){
                        $sort_ids = implode(',',$sort_ids);
                        Db::connect('db2')->name('mill_order_archives')->where("id in ({$sort_ids})")->delete();//删除已存在的数据
                        Db::connect('db2')->name('mill_order_archives_give')->where("archives_id in ({$sort_ids})")->delete();//删除已存在的数据
                    }
                }

                //当前序号，从1开始
                $sort = 0;
                $add_arr = [];
                //同步更新产品明细
                $give_list = Db::name('mill_order_archives_give')
                    ->alias('g')
                    ->field('g.*,a.sort as a_sort')
                    ->leftJoin('mill_order_archives a','a.id = g.archives_id')
                    ->where(['g.order_id'=>$order_id_db])
                    ->order('g.id asc')
                    ->select();
                if(!empty($give_list)){
                    foreach ($give_list as $gv){

                        $gv['order_id'] = $order['id'];
                        $gv['order_pid'] = $order['order_pid'];
                        $gv['archives_id'] = Db::connect('db2')->name('mill_order_archives')->where(['order_id'=>$id,'sort'=>$gv['a_sort']])->value('id');

                        $give_id_db2 = Db::connect('db2')->name('mill_order_archives_give')->where(['order_id'=>$id,'sort'=>$gv['sort'],'counter'=>$gv['counter']])->value('id');

                        unset($gv['id']);
                        //录入
                        if(empty($give_id_db2)){
                            unset($gv['id']);
                            $add_arr[] = $gv;
                            $sort++;
                        }
                        //更新
                        else Db::connect('db2')->name('mill_order_archives_give')->where(['id'=>$give_id_db2])->update($gv);
                    }
                }

                //2000条数据为一批次
                $add_arr_i = intval($sort/2000)+1;
                if($add_arr_i > 1)$add_arr_arr = array_chunk($add_arr,$add_arr_i);
                else $add_arr_arr = [$add_arr];
                //分批次插入
                foreach ($add_arr_arr as $varr){
                    Db::connect('db2')->name('mill_order_archives_give')->insertAll($varr);
                }

                //更新档案号
                Db::connect('db2')->name('mill_order_archives')->where(['order_id'=>$id])->update(['archives_sn'=>$data['archives_sn']]);
                Db::connect('db2')->name('mill_order_archives_give')->where(['order_id'=>$id])->update(['archives_sn'=>$data['archives_sn']]);

                if($is_pid && $__encry__){
                    Db::connect('db2')->name('mill_order_archives')->where(['encry'=>$__encry__])->update(['order_id'=>$id,'encry'=>'']);
                }
            }

        }
    }

    //同步更新扫码产品进度，仅限喷涂
    public static function giveEdit($give_sn = ''){

        if(empty(self::$is_open))return '';

        $save = [
            'status_flow'   =>  '_1|69_,_2|69_,_3|69_',
            'status'        =>  3,
            'status1_time'  =>  time(),
            'status2_time'  =>  time(),
            'status3_time'  =>  time(),
        ];
        $status = Db::connect('db2')->name('mill_order_archives_give')->where(['give_sn'=>$give_sn])->value('status');

        if($status < 3)Db::connect('db2')->name('mill_order_archives_give')->where(['give_sn'=>$give_sn])->update($save);

    }

}
